package com.ekuaitu.kuaitu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.bean.MyRouteBean;
import com.ekuaitu.kuaitu.map.GeocodeSearchViewUtils;
import com.ekuaitu.kuaitu.utils.TimeUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by CaoJing on 2016/10/5.
 */
public class MyRouteLvAdapter extends BaseAdapter {
    private List<MyRouteBean.AttachmentBean.CompleteOrderlListBean> mlist;
    private Context mContext;
    GeocodeSearchViewUtils geocodeSearchViewUtils1 = new GeocodeSearchViewUtils();

    public MyRouteLvAdapter(List<MyRouteBean.AttachmentBean.CompleteOrderlListBean> list, Context context) {
        this.mlist = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mlist != null ? mlist.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mlist != null ? mlist.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemview = LayoutInflater.from(mContext).inflate(R.layout.item_my_route, null);
        AutoUtils.autoSize(itemview);
        TextView routeItemTvStatus = (TextView) itemview.findViewById(R.id.route_item_tv_status);
        TextView routeItemTime = (TextView) itemview.findViewById(R.id.route_item_time);
        TextView routeItemUsedTime = (TextView) itemview.findViewById(R.id.route_item_usedTime);
        TextView routeItemMoney = (TextView) itemview.findViewById(R.id.route_item_money);
        View Routeline1 = itemview.findViewById(R.id.route_line1);
        View Routeline = itemview.findViewById(R.id.route_line);
        ImageView routeItemIvWay = (ImageView) itemview.findViewById(R.id.route_item_iv_way);
        TextView routeItemBrand = (TextView) itemview.findViewById(R.id.route_item_brand);
        TextView routeItemId = (TextView) itemview.findViewById(R.id.route_item_id);
        TextView routeItemTvStart = (TextView) itemview.findViewById(R.id.route_item_tv_start);
        TextView routeItemTvEnd = (TextView) itemview.findViewById(R.id.route_item_tv_end);

        MyRouteBean.AttachmentBean.CompleteOrderlListBean item = mlist.get(position);
        routeItemTvStatus.setText(getOrderStatus(item.getStatus()));
        routeItemTime.setText(TimeUtils.getFormatDate(item.getOrderTime() + "", "M月d日H:mm"));


        if (item.getBusinessType() == 1 && (item.getStatus() == 17 || item.getStatus() == 103)) {
            routeItemUsedTime.setText("15分钟");
        } else {
            routeItemUsedTime.setText("耗时：" + TimeUtils.formatMinPlusOne((item.getEndBillingTime() - item.getStartBillingTime()) / 1000 + ""));
        }
        DecimalFormat df = new DecimalFormat("0.00");
        routeItemMoney.setText("¥" + df.format(item.getRealPrice()));

        if (position == 0) {
            Routeline1.setVisibility(View.GONE);
            Routeline.setVisibility(View.VISIBLE);
        } else if (position == mlist.size() - 1) {
            Routeline.setVisibility(View.GONE);
            Routeline1.setVisibility(View.VISIBLE);
        } else {
            Routeline1.setVisibility(View.VISIBLE);
            Routeline.setVisibility(View.VISIBLE);
        }
        if (item.getBusinessType() == 1 && item.getCarModel() != null) {
            //汽车图标
            routeItemIvWay.setImageResource(R.drawable.icon_car);
            //汽车品牌
            if (item.getCarModel().getBrand() != null && item.getCarModel().getModel() != null) {
                routeItemBrand.setText(item.getCarModel().getBrand() + item.getCarModel().getModel());
            }
            //汽车牌照
            if (item.getCarModel().getLicenseTag() != null) {
                routeItemId.setText(item.getCarModel().getLicenseTag());
            }
            //起点
            routeItemTvStart.setText(item.getOnLocationName());
            //终点
            if (item.getDownLocationName() != null) {
                routeItemTvEnd.setText(item.getDownLocationName());
            }
        } else if (item.getBusinessType() == 2 && item.getBikeModel() != null) {
            //单车图标
            routeItemIvWay.setImageResource(R.drawable.icon_bicycle);
            //单车无品牌
            routeItemBrand.setText("");
            //单车编号
            if (item.getBikeModel().getLicenseTag() != null) {
                routeItemId.setText(item.getBikeModel().getLicenseTag());
            }
            //单车起点和终点
            routeItemTvStart.setText(item.getOnLocationName());
            routeItemTvEnd.setText(item.getDownLocationName());
        }
        return itemview;
    }

    public String getOrderStatus(int code) {
        String orderStatus = null;
        switch (code) {
            case 0:
                orderStatus = "未取车";
                break;
            case 10:
                orderStatus = "用车中";
                break;
            case 15:
            case 16:
            case 17:
                orderStatus = "未结算";
                break;
            case 101:
                orderStatus = "已完成";
                break;
            case 102:
                orderStatus = "手动取消";
                break;
            case 103:
                orderStatus = "自动取消";
                break;
            case 104:
                orderStatus = "客服取消";
                break;
        }
        return orderStatus;
    }
}

